/**
 * Copyright 2024/3/23 the original brandnewdata.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zlb.envmodify.transform.config;

import com.zlb.envmodify.entity.MethodOverWriteConfig;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @ClassName ModifyClassConfig
 * @Author zhulb
 * @Date 2024/3/23 0:55
 * @Version 0.0.1
 */
public class ModifyClassConfig {

    /**
     * 方法重写配置信息
     */
    Map<String, Map<String, List<MethodOverWriteConfig>>> methodOverWriteConfigs;

    public ModifyClassConfig(String configFilePath) {
        this.readConfig(configFilePath);
    }

    private void readConfig(String configFilePath) {
        try {
            this.methodOverWriteConfigs = new HashMap<>();
            String line, oldLine = null, className, methodName, type, methodDesc, target, needCopyMethod;
            Map<String, List<MethodOverWriteConfig>> classMethods;
            List<MethodOverWriteConfig> configs;
            MethodOverWriteConfig overWriteMethodConfig;
            BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(configFilePath)));
            line = bufferedReader.readLine();
            while (line != null) {
                if (line.startsWith("---") && (className = bufferedReader.readLine()) != null && !className.trim().isEmpty()
                        && (methodName = bufferedReader.readLine()) != null && !methodName.trim().isEmpty()
                        && (type = bufferedReader.readLine()) != null && !type.trim().isEmpty()
                        && (methodDesc = bufferedReader.readLine()) != null && !methodDesc.trim().isEmpty()
                        && (needCopyMethod = bufferedReader.readLine()) != null && !needCopyMethod.trim().isEmpty()
                        && (target = bufferedReader.readLine()) != null && !target.trim().isEmpty()) {
                    if ((classMethods = this.methodOverWriteConfigs.get(className)) == null) {
                        classMethods = new HashMap<String, List<MethodOverWriteConfig>>();
                        this.methodOverWriteConfigs.put(className, classMethods);
                    }
                    if ((configs = classMethods.get(methodName)) == null) {
                        configs = new LinkedList<>();
                        classMethods.put(methodName, configs);
                    }

                    StringBuffer sb = new StringBuffer();
                    while ((line = bufferedReader.readLine()) != null && !line.startsWith("---")) {
                        sb.append(line + "\n");
                    }
                    overWriteMethodConfig = new MethodOverWriteConfig();
                    overWriteMethodConfig.setType(type);
                    overWriteMethodConfig.setMethodDesc(methodDesc);
                    overWriteMethodConfig.setOverWriteText(sb.toString());
                    overWriteMethodConfig.setTarget(target);
                    overWriteMethodConfig.setNeedCopyMethod(Boolean.valueOf(needCopyMethod));

                    configs.add(overWriteMethodConfig);
                }
                if (oldLine == line) {
                    break;
                }
                oldLine = line;
                line = line == null ? line : line.startsWith("---") ? line : bufferedReader.readLine();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Map<String, Map<String, List<MethodOverWriteConfig>>> getMethodOverWriteConfigs() {
        return this.methodOverWriteConfigs;
    }


}
